using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library;
using Framework.Library.Components.Commands;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;

namespace Portal.Administrator.UserControls
{
	public partial class TabPanel : AuthenticatedControl
	{
        private int portalid = 0;
		private string cmd = string.Empty;
		protected void Page_Load(object sender, EventArgs e)
		{
            portalid = ConvertUtility.ToInt32(Request.QueryString["portalid"]);
			cmd = ConvertUtility.ToString(Request.QueryString["cmd"]);
			if (cmd == string.Empty) return;
			if (!Page.IsPostBack)
			{
				CommandInfo command = CommandController.GetCommand(cmd);
				if (command == null) return;
				DataTable dtCommandsForCurUser = null;
                if (!CurrentUser.IsSuperAdmin)
                {
                    dtCommandsForCurUser = CommandController.GetCommandsForUserByPortalID(CurrentUser.UserID, portalid);
                }
                else
                {
                    dtCommandsForCurUser = CommandController.GetCommands();
                }
				DataRow[] drCommands = dtCommandsForCurUser.Select("CommandParentID = " + command.CommandParentID + " AND CommandEnable = 1");
				rptMenu.DataSource = drCommands;
				rptMenu.ItemDataBound += new RepeaterItemEventHandler(rptMenu_ItemDataBound);
				rptMenu.DataBind();
			}
		}
        
		private void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				DataRow curData = (DataRow)e.Item.DataItem;
				HyperLink hlCmd = (HyperLink)e.Item.FindControl("hlCmd");
				hlCmd.Text = curData["CommandName"].ToString();

				Literal litStyle = (Literal)e.Item.FindControl("litStyle");
                if (curData["CommandKey"].ToString() == cmd)
                {
                    litStyle.Text = "Tab";
                }
                else
                {
                    litStyle.Text = "Tabvisited";
                    if (curData["CommandUrl"].ToString() != string.Empty)
                    {
                        hlCmd.NavigateUrl = curData["CommandUrl"].ToString();
                    }
                    else if (curData["CommandKey"].ToString() != string.Empty)
                    {
                        hlCmd.NavigateUrl = AppEnv.AdminUrlParams(curData["CommandKey"].ToString()) + curData["CommandParams"].ToString(); 
                    }
                }
			}
		}
	}
}